<?php


use think\Request;

/**
 * 状态
 * @param $status
 * @return string
 */
function status($status, $id)
{
    $controller = Request::instance()->controller();
    switch ($status) {
        case 0:
            $result = "<a href=\"javascript:;\"  onclick=\"tijiao('" . url($controller . '/resume') . "',{$id},{$status},this)\"  class='label label-success radius'  title=\"点击恢复\">恢复</a>";
            break;
        case 1:
            $result = "<a href=\"javascript:;\"  onclick=\"tijiao('" . url($controller . '/forbid') . "',{$id},{$status},this)\"  class='label label-danger radius' title=\"点击禁用\">禁用</a>";
            break;
        case 2:
            $result = "不通过";
            break;
        default:
            $result = "";
            break;
    }
    return $result;
}
/*
 * $data array 返回数据
 * $status string 状态
 * $msg string 提示语
 */
function ajaxReturn($data = [], $status = 1, $msg = '')
{
    header('Content-Type:application/json; charset=utf-8');
    $data = $data;
    $data['status'] = $status;
    if ($msg != '') {
        $data['msg'] = $msg;
    }
    exit(json_encode($data));
}



/**
 * 状态
 * @param $status
 * @return string
 */
function check($status)
{
    switch ($status) {
        case 0:
            $result = '<a class=\'label label-danger radius\' >未阅</a>';
            break;
        case 1:
            $result = '<a class=\'label label-success radius\' >已阅</a>';
            break;
        default:
            $result = "";
            break;
    }
    return $result;
}

/**
 * 推荐
 * @param $status
 * @return string
 */
function tuijian($status, $id)
{
    $controller = Request::instance()->controller();
    switch ($status) {
        case 0:
            $result = "<a href=\"javascript:;\"  onclick=\"tijiao('" . url($controller . '/Rrecommended') . "',{$id})\"  class='label label-success radius'  title=\"点击推荐\">推荐</a>";
            break;
        case 1:
            $result = "<a href=\"javascript:;\"  onclick=\"tijiao('" . url($controller . '/NotRrecommended') . "',{$id})\"  class='label label-danger radius' title=\"点击不推荐\">不推荐</a>";
            break;
        default:
            $result = "";
            break;
    }
    return $result;
}
/**
 * 热销
 * @param $status
 * @return string
 */
function hot($status, $id)
{
    $controller = Request::instance()->controller();
    switch ($status) {
        case 0:
            $result = "<a href=\"javascript:;\"  onclick=\"tijiao('" . url($controller . '/hotup') . "',{$id})\"  class='label label-success radius'  title=\"点击热销\">热销</a>";
            break;
        case 1:
            $result = "<a href=\"javascript:;\"  onclick=\"tijiao('" . url($controller . '/hotdu') . "',{$id})\"  class='label label-danger radius' title=\"点击取消热销\">取消热销</a>";
            break;
        default:
            $result = "";
            break;
    }
    return $result;
}

/**
 * 头条
 * @param $status
 * @return string
 */
function top($status, $id)
{
    $controller = Request::instance()->controller();
    switch ($status) {
        case 0:
            $result = "<a href=\"javascript:;\"  onclick=\"tijiao('" . url($controller . '/topup') . "',{$id})\"  class='label label-success radius'  title=\"点击头条\">设为头条</a>";
            break;
        case 1:
            $result = "<a href=\"javascript:;\"  onclick=\"tijiao('" . url($controller . '/topdu') . "',{$id})\"  class='label label-danger radius' title=\"点击取消头条\">取消头条</a>";
            break;
        default:
            $result = "";
            break;
    }
    return $result;
}


/**
 * 排序
 * @param $status
 * @return string
 */
function sortby($status, $id)
{
    $controller = Request::instance()->controller();
    $result = " <input name=\"sort\" class=\"input-text\" value=\"{$status}\" onblur=\"change('" . url($controller . '/sort') . "',this,{$id})\"  type=\"number\"/>";
    return $result;
}

if (!function_exists('delDir')) {
    /**
     * 递归删除文件夹
     *
     * @param string $path 目录路径
     * @param boolean $delDir 是否删除空目录
     * @return boolean
     */
    function delDir($path, $delDir = FALSE)
    {
        if (!is_dir($path))
            return FALSE;
        $handle = @opendir($path);
        if ($handle) {
            while (false !== ($item = readdir($handle))) {
                if ($item != "." && $item != "..")
                    is_dir("$path/$item") ? delDir("$path/$item", $delDir) : @unlink("$path/$item");
            }
            closedir($handle);
            if ($delDir) {
                return @rmdir($path);
            }
        } else {
            if (file_exists($path)) {
                return @unlink($path);
            } else {
                return FALSE;
            }
        }
    }
}

/**
 * 模板选择
 * @param $style
 * @return array
 */
function templateList($style)
{
    $arr = cache('template' .$style);
    if (empty($arr)) {

        $list = glob(ROOT_PATH . 'template' . DIRECTORY_SEPARATOR . $style . DIRECTORY_SEPARATOR . '*', GLOB_ONLYDIR);
        $arr = [];

        foreach ($list as $key => $v) {
            $dirname = basename($v);
            if (file_exists($v . DIRECTORY_SEPARATOR . 'config.php')) {
                $arr[$key] = (include $v . DIRECTORY_SEPARATOR . 'config.php');
            } else {
                $arr[$key]['name'] = $dirname;
            }
            $arr[$key]['dirname'] = $dirname;
        }
        cache('template' .$style,$arr,3600);
    }

    return $arr;
}


/**
 * 表格排序筛选
 * @param string $name 单元格名称
 * @param string $field 排序字段
 * @return string
 */
function sort_by($name, $field = '')
{
    $sort = Request::instance()->param('sort');
    $param = Request::instance()->get();
    $param['sort'] = ($sort == 'asc' ? 'desc' : 'asc');
    $param['order'] = $field;
    $param['cid'] = Request::instance()->param('cid');
    unset($param['page']);
    $get = null;
    $param = array_unique($param);
    foreach ($param as $key => $value) {
        $get .= '&' . $key . '=' . $value;
    }


    $url = Request::instance()->baseUrl() . '?' . $get;
    //    $url = Url(Request::instance()->action(), $param);

    return Request::instance()->param('order') == $field ?
        "<a href='{$url}' title='点击排序' class='sorting-box sorting-{$sort}'>{$name}</a>" : "<a href='{$url}' title='点击排序' class='sorting-box sorting'>{$name}</a>";
}


if (!function_exists('get_total_arc')) {
    /**
     * 获取指定栏目的文档数
     */
    function get_total_arc($typeid, $model)
    {
        $total = 0;
        $current_channel = db($model)->where('cid', $typeid)->where('lang', cookie('think_var_admin'))->count();
        return $current_channel;
        exit();
        $allow_release_channel = config('global.allow_release_channel');
        if (in_array($current_channel, $allow_release_channel)) { // 能发布文档的模型
            $result = model('Arctype')->getHasChildren($typeid);
            $typeidArr = get_arr_column($result, 'id');
            $map = array(
                'typeid'    => array('IN', $typeidArr),
                'channel'    => array('eq', $current_channel),
                'is_del'    => 0, // 回收站功能
            );
            $total = db('archives')->where($map)->count();
        } elseif ($current_channel == 8) { // 留言模型
            $total = db('massage')->where(array('cid' => array('eq', $typeid)))->count();
        }

        return $total;
    }
}

if (!function_exists('get_total_massage')) {
    /**
     * 获取指定栏目的文档数
     */
    function get_total_massage($cid)
    {
        $current_channel = 0;
        $current_channel = db('massage')->where('cid', $cid)->where('lang', cookie('think_var_admin'))->count();
        return $current_channel;
    }
}

if (!function_exists('format_bytes')) {
    /**
     * 格式化字节大小
     *
     * @param  number $size      字节数
     * @param  string $delimiter 数字和单位分隔符
     * @return string            格式化后的带单位的大小
     */
    function format_bytes($size, $delimiter = '')
    {
        $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
        for ($i = 0; $size >= 1024 && $i < 5; $i++) $size /= 1024;
        return round($size, 2) . $delimiter . $units[$i];
    }
}
